from django.db import models
from apps.users.models import User

class WallPost(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='wall_posts', verbose_name='用户')
    content = models.TextField(max_length=655350, verbose_name='内容')
    tags = models.JSONField(default=list, blank=True, verbose_name='标签')
    duration = models.FloatField(default=0, verbose_name='学习时长')
    score = models.IntegerField(default=0, verbose_name='自评分数')
    likes_count = models.IntegerField(default=0, verbose_name='点赞数')
    comments_count = models.IntegerField(default=0, verbose_name='评论数')
    image = models.CharField(max_length=255, null=True, blank=True, verbose_name='图片路径')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        db_table = 'wall_post'
        ordering = ['-created_at']
        verbose_name = '学习记录'
        verbose_name_plural = verbose_name

class WallComment(models.Model):
    post = models.ForeignKey(WallPost, on_delete=models.CASCADE, related_name='comments')
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'wall_comment'
        ordering = ['created_at']

class PostLike(models.Model):
    post = models.ForeignKey(WallPost, on_delete=models.CASCADE, related_name='likes')
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'wall_post_like'
        unique_together = ('post', 'user')  # 确保每个用户只能给一个帖子点赞一次
